fn main() {
    let v = vec![1,8,6,2,5,4,8,3,7];
    let ans = Solution::max_area(v);
    println!("{}", ans);
    let v = vec![1,8,6,2,5,4,8,3,7];
    let ans = Solution2::max_area(v);
    println!("{}", ans);
}

struct Solution;

impl Solution {
    pub fn max_area(height: Vec<i32>) -> i32 {
        let mut max = 0;
        let length = height.len();
        for i in 0..(length - 1) {
            for j in (i + 1)..length {
                let min = if height[i] < height[j] {
                    height[i]
                } else {
                    height[j]
                };
                let v = (j - i) as i32 * min;
                if v > max {
                    max = v;
                }
            }
        }
        max
    }
}

struct Solution2;

impl Solution2 {
    pub fn max_area(height: Vec<i32>) -> i32 {
        let mut ans = 0;
        let mut l = 0;
        let mut r = height.len() - 1;
        while l < r {
            let v_l = height[l];
            let v_r = height[r];
            ans = ans.max(v_l.min(v_r) * (r - l) as i32);
            if v_l < v_r {
                l = l + 1;
            } else {
                r = r - 1;
            }
        }
        ans
    }
}
